home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / perl40_2.zip / REGEXEC.C < prev    next >
C/C++ Source or Header  |  1991-11-28  |  22KB  |  943 lines

  1. /* NOTE: this is derived from Henry Spencer's regexp code, and should not
  2.  * confused with the original package (see point 3 below).  Thanks, Henry!
  3.  */
  4.  
  5.  
  6. /* Additional note: this code is very heavily munged from Henry's version
  7.  * in places.  In some spots I've traded clarity for efficiency, so don't
  8.  * blame Henry for some of the lack of readability.
  9.  */
  10.  
  11.  
  12. /* $RCSfile: regexec.c,v $$Revision: 4.0.1.3 $$Date: 91/11/05 18:23:55 $
  13.  *
  14.  * $Log:    regexec.c,v $
  15.  * Revision 4.0.1.3  91/11/05  18:23:55  lwall
  16.  * patch11: prepared for ctype implementations that don't define isascii()
  17.  * patch11: initial .* in pattern had dependency on value of $*
  18.  *
  19.  * Revision 4.0.1.2  91/06/07  11:50:33  lwall
  20.  * patch4: new copyright notice
  21.  * patch4: // wouldn't use previous pattern if it started with a null character
  22.  *
  23.  * Revision 4.0.1.1  91/04/12  09:07:39  lwall
  24.  * patch1: regexec only allocated space for 9 subexpresssions
  25.  *
  26.  * Revision 4.0  91/03/20  01:39:16  lwall
  27.  * 4.0 baseline.
  28.  *
  29.  */
  30. /*SUPPRESS 112*/
  31. /*
  32.  * regcomp and regexec -- regsub and regerror are not used in perl
  33.  *
  34.  *    Copyright (c) 1986 by University of Toronto.
  35.  *    Written by Henry Spencer.  Not derived from licensed software.
  36.  *
  37.  *    Permission is granted to anyone to use this software for any
  38.  *    purpose on any computer system, and to redistribute it freely,
  39.  *    subject to the following restrictions:
  40.  *
  41.  *    1. The author is not responsible for the consequences of use of
  42.  *        this software, no matter how awful, even if they arise
  43.  *        from defects in it.
  44.  *
  45.  *    2. The origin of this software must not be misrepresented, either
  46.  *        by explicit claim or by omission.
  47.  *
  48.  *    3. Altered versions must be plainly marked as such, and must not
  49.  *        be misrepresented as being the original software.
  50.  *
  51.  ****    Alterations to Henry's code are...
  52.  ****
  53.  ****    Copyright (c) 1991, Larry Wall
  54.  ****
  55.  ****    You may distribute under the terms of either the GNU General Public
  56.  ****    License or the Artistic License, as specified in the README file.
  57.  *
  58.  * Beware that some of this code is subtly aware of the way operator
  59.  * precedence is structured in regular expressions.  Serious changes in
  60.  * regular-expression syntax might require a total rethink.
  61.  */
  62. #include "EXTERN.h"
  63. #include "perl.h"
  64. #include "regcomp.h"
  65.  
  66.  
  67. #ifndef STATIC
  68. #define    STATIC    static
  69. #endif
  70.  
  71.  
  72. #ifdef DEBUGGING
  73. int regnarrate = 0;
  74. #endif
  75.  
  76.  
  77. /*
  78.  * regexec and friends
  79.  */
  80.  
  81.  
  82. /*
  83.  * Global work variables for regexec().
  84.  */
  85. static char *regprecomp;
  86. static char *reginput;        /* String-input pointer. */
  87. static char regprev;        /* char before regbol, \n if none */
  88. static char *regbol;        /* Beginning of input, for ^ check. */
  89. static char *regeol;        /* End of input, for $ check. */
  90. static char **regstartp;    /* Pointer to startp array. */
  91. static char **regendp;        /* Ditto for endp. */
  92. static char *reglastparen;    /* Similarly for lastparen. */
  93. static char *regtill;
  94.  
  95.  
  96. static int regmyp_size = 0;
  97. static char **regmystartp = Null(char**);
  98. static char **regmyendp   = Null(char**);
  99.  
  100.  
  101. /*
  102.  * Forwards.
  103.  */
  104. STATIC int regtry();
  105. STATIC int regmatch();
  106. STATIC int regrepeat();
  107.  
  108.  
  109. extern int multiline;
  110.  
  111.  
  112. /*
  113.  - regexec - match a regexp against a string
  114.  */
  115. int
  116. regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
  117. register regexp *prog;
  118. char *stringarg;
  119. register char *strend;    /* pointer to null at end of string */
  120. char *strbeg;    /* real beginning of string */
  121. int minend;    /* end of match must be at least minend after stringarg */
  122. STR *screamer;
  123. int safebase;    /* no need to remember string in subbase */
  124. {
  125.     register char *s;
  126.     register int i;
  127.     register char *c;
  128.     register char *string = stringarg;
  129.     register int tmp;
  130.     int minlen = 0;        /* must match at least this many chars */
  131.     int dontbother = 0;    /* how many characters not to try at end */
  132.  
  133.  
  134.     /* Be paranoid... */
  135.     if (prog == NULL || string == NULL) {
  136.         fatal("NULL regexp parameter");
  137.         return(0);
  138.     }
  139.  
  140.  
  141.     if (string == strbeg)    /* is ^ valid at stringarg? */
  142.         regprev = '\n';
  143.     else {
  144.         regprev = stringarg[-1];
  145.         if (!multiline && regprev == '\n')
  146.         regprev = '\0';        /* force ^ to NOT match */
  147.     }
  148.     regprecomp = prog->precomp;
  149.     /* Check validity of program. */
  150.     if (UCHARAT(prog->program) != MAGIC) {
  151.         FAIL("corrupted regexp program");
  152.     }
  153.  
  154.  
  155.     if (prog->do_folding) {
  156.         safebase = FALSE;
  157.         i = strend - string;
  158.         New(1101,c,i+1,char);
  159.         (void)bcopy(string, c, i+1);
  160.         string = c;
  161.         strend = string + i;
  162.         for (s = string; s < strend; s++)
  163.             if (isUPPER(*s))
  164.                 *s = tolower(*s);
  165.     }
  166.  
  167.  
  168.     /* If there is a "must appear" string, look for it. */
  169.     s = string;
  170.     if (prog->regmust != Nullstr &&
  171.         (!(prog->reganch & ROPT_ANCH)
  172.          || (multiline && prog->regback >= 0)) ) {
  173.         if (stringarg == strbeg && screamer) {
  174.             if (screamfirst[prog->regmust->str_rare] >= 0)
  175.                 s = screaminstr(screamer,prog->regmust);
  176.             else
  177.                 s = Nullch;
  178.         }
  179. #ifndef lint
  180.         else
  181.             s = fbminstr((unsigned char*)s, (unsigned char*)strend,
  182.                 prog->regmust);
  183. #endif
  184.         if (!s) {
  185.             ++prog->regmust->str_u.str_useful;    /* hooray */
  186.             goto phooey;    /* not present */
  187.         }
  188.         else if (prog->regback >= 0) {
  189.             s -= prog->regback;
  190.             if (s < string)
  191.                 s = string;
  192.             minlen = prog->regback + prog->regmust->str_cur;
  193.         }
  194.         else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
  195.             str_free(prog->regmust);
  196.             prog->regmust = Nullstr;    /* disable regmust */
  197.             s = string;
  198.         }
  199.         else {
  200.             s = string;
  201.             minlen = prog->regmust->str_cur;
  202.         }
  203.     }
  204.  
  205.  
  206.     /* Mark beginning of line for ^ . */
  207.     regbol = string;
  208.  
  209.  
  210.     /* Mark end of line for $ (and such) */
  211.     regeol = strend;
  212.  
  213.  
  214.     /* see how far we have to get to not match where we matched before */
  215.     regtill = string+minend;
  216.  
  217.  
  218.     /* Allocate our backreference arrays */
  219.     if ( regmyp_size < prog->nparens + 1 ) {
  220.         /* Allocate or enlarge the arrays */
  221.         regmyp_size = prog->nparens + 1;
  222.         if ( regmyp_size < 10 ) regmyp_size = 10;    /* minimum */
  223.         if ( regmystartp ) {
  224.         /* reallocate larger */
  225.         Renew(regmystartp,regmyp_size,char*);
  226.         Renew(regmyendp,  regmyp_size,char*);
  227.         }
  228.         else {
  229.         /* Initial allocation */
  230.         New(1102,regmystartp,regmyp_size,char*);
  231.         New(1102,regmyendp,  regmyp_size,char*);
  232.         }
  233.     
  234.     }
  235.  
  236.  
  237.     /* Simplest case:  anchored match need be tried only once. */
  238.     /*  [unless multiline is set] */
  239.     if (prog->reganch & ROPT_ANCH) {
  240.         if (regtry(prog, string))
  241.             goto got_it;
  242.         else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
  243.             if (minlen)
  244.                 dontbother = minlen - 1;
  245.             strend -= dontbother;
  246.             /* for multiline we only have to try after newlines */
  247.             if (s > string)
  248.                 s--;
  249.             while (s < strend) {
  250.                 if (*s++ == '\n') {
  251.                 if (s < strend && regtry(prog, s))
  252.                     goto got_it;
  253.                 }
  254.             }
  255.         }
  256.         goto phooey;
  257.     }
  258.  
  259.  
  260.     /* Messy cases:  unanchored match. */
  261.     if (prog->regstart) {
  262.         if (prog->reganch & ROPT_SKIP) {  /* we have /x+whatever/ */
  263.             /* it must be a one character string */
  264.             i = prog->regstart->str_ptr[0];
  265.             while (s < strend) {
  266.                 if (*s == i) {
  267.                     if (regtry(prog, s))
  268.                         goto got_it;
  269.                     s++;
  270.                     while (s < strend && *s == i)
  271.                     s++;
  272.                 }
  273.                 s++;
  274.             }
  275.         }
  276.         else if (prog->regstart->str_pok == 3) {
  277.             /* We know what string it must start with. */
  278. #ifndef lint
  279.             while ((s = fbminstr((unsigned char*)s,
  280.               (unsigned char*)strend, prog->regstart)) != NULL)
  281. #else
  282.             while (s = Nullch)
  283. #endif
  284.             {
  285.                 if (regtry(prog, s))
  286.                     goto got_it;
  287.                 s++;
  288.             }
  289.         }
  290.         else {
  291.             c = prog->regstart->str_ptr;
  292.             while ((s = ninstr(s, strend,
  293.               c, c + prog->regstart->str_cur )) != NULL) {
  294.                 if (regtry(prog, s))
  295.                     goto got_it;
  296.                 s++;
  297.             }
  298.         }
  299.         goto phooey;
  300.     }
  301.     /*SUPPRESS 560*/
  302.     if (c = prog->regstclass) {
  303.         int doevery = (prog->reganch & ROPT_SKIP) == 0;
  304.  
  305.  
  306.         if (minlen)
  307.             dontbother = minlen - 1;
  308.         strend -= dontbother;    /* don't bother with what can't match */
  309.         tmp = 1;
  310.         /* We know what class it must start with. */
  311.         switch (OP(c)) {
  312.         case ANYOF:
  313.             c = OPERAND(c);
  314.             while (s < strend) {
  315.                 i = UCHARAT(s);
  316.                 if (!(c[i >> 3] & (1 << (i&7)))) {
  317.                     if (tmp && regtry(prog, s))
  318.                         goto got_it;
  319.                     else
  320.                         tmp = doevery;
  321.                 }
  322.                 else
  323.                     tmp = 1;
  324.                 s++;
  325.             }
  326.             break;
  327.         case BOUND:
  328.             if (minlen)
  329.             dontbother++,strend--;
  330.             if (s != string) {
  331.             i = s[-1];
  332.             tmp = isALNUM(i);
  333.             }
  334.             else
  335.             tmp = isALNUM(regprev);    /* assume not alphanumeric */
  336.             while (s < strend) {
  337.                 i = *s;
  338.                 if (tmp != isALNUM(i)) {
  339.                     tmp = !tmp;
  340.                     if (regtry(prog, s))
  341.                         goto got_it;
  342.                 }
  343.                 s++;
  344.             }
  345.             if ((minlen || tmp) && regtry(prog,s))
  346.                 goto got_it;
  347.             break;
  348.         case NBOUND:
  349.             if (minlen)
  350.             dontbother++,strend--;
  351.             if (s != string) {
  352.             i = s[-1];
  353.             tmp = isALNUM(i);
  354.             }
  355.             else
  356.             tmp = isALNUM(regprev);    /* assume not alphanumeric */
  357.             while (s < strend) {
  358.                 i = *s;
  359.                 if (tmp != isALNUM(i))
  360.                     tmp = !tmp;
  361.                 else if (regtry(prog, s))
  362.                     goto got_it;
  363.                 s++;
  364.             }
  365.             if ((minlen || !tmp) && regtry(prog,s))
  366.                 goto got_it;
  367.             break;
  368.         case ALNUM:
  369.             while (s < strend) {
  370.                 i = *s;
  371.                 if (isALNUM(i)) {
  372.                     if (tmp && regtry(prog, s))
  373.                         goto got_it;
  374.                     else
  375.                         tmp = doevery;
  376.                 }
  377.                 else
  378.                     tmp = 1;
  379.                 s++;
  380.             }
  381.             break;
  382.         case NALNUM:
  383.             while (s < strend) {
  384.                 i = *s;
  385.                 if (!isALNUM(i)) {
  386.                     if (tmp && regtry(prog, s))
  387.                         goto got_it;
  388.                     else
  389.                         tmp = doevery;
  390.                 }
  391.                 else
  392.                     tmp = 1;
  393.                 s++;
  394.             }
  395.             break;
  396.         case SPACE:
  397.             while (s < strend) {
  398.                 if (isSPACE(*s)) {
  399.                     if (tmp && regtry(prog, s))
  400.                         goto got_it;
  401.                     else
  402.                         tmp = doevery;
  403.                 }
  404.                 else
  405.                     tmp = 1;
  406.                 s++;
  407.             }
  408.             break;
  409.         case NSPACE:
  410.             while (s < strend) {
  411.                 if (!isSPACE(*s)) {
  412.                     if (tmp && regtry(prog, s))
  413.                         goto got_it;
  414.                     else
  415.                         tmp = doevery;
  416.                 }
  417.                 else
  418.                     tmp = 1;
  419.                 s++;
  420.             }
  421.             break;
  422.         case DIGIT:
  423.             while (s < strend) {
  424.                 if (isDIGIT(*s)) {
  425.                     if (tmp && regtry(prog, s))
  426.                         goto got_it;
  427.                     else
  428.                         tmp = doevery;
  429.                 }
  430.                 else
  431.                     tmp = 1;
  432.                 s++;
  433.             }
  434.             break;
  435.         case NDIGIT:
  436.             while (s < strend) {
  437.                 if (!isDIGIT(*s)) {
  438.                     if (tmp && regtry(prog, s))
  439.                         goto got_it;
  440.                     else
  441.                         tmp = doevery;
  442.                 }
  443.                 else
  444.                     tmp = 1;
  445.                 s++;
  446.             }
  447.             break;
  448.         }
  449.     }
  450.     else {
  451.         if (minlen)
  452.             dontbother = minlen - 1;
  453.         strend -= dontbother;
  454.         /* We don't know much -- general case. */
  455.         do {
  456.             if (regtry(prog, s))
  457.                 goto got_it;
  458.         } while (s++ < strend);
  459.     }
  460.  
  461.  
  462.     /* Failure. */
  463.     goto phooey;
  464.  
  465.  
  466.     got_it:
  467.     if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
  468.         strend += dontbother;    /* uncheat */
  469.         if (safebase)            /* no need for $digit later */
  470.             s = strbeg;
  471.         else if (strbeg != prog->subbase) {
  472.             i = strend - string + (stringarg - strbeg);
  473.             s = nsavestr(strbeg,i);    /* so $digit will work later */
  474.             if (prog->subbase)
  475.                 Safefree(prog->subbase);
  476.             prog->subbeg = prog->subbase = s;
  477.             prog->subend = s+i;
  478.         }
  479.         else
  480.             s = prog->subbase;
  481.         s += (stringarg - strbeg);
  482.         for (i = 0; i <= prog->nparens; i++) {
  483.             if (prog->endp[i]) {
  484.                 prog->startp[i] = s + (prog->startp[i] - string);
  485.                 prog->endp[i] = s + (prog->endp[i] - string);
  486.             }
  487.         }
  488.         if (prog->do_folding)
  489.             Safefree(string);
  490.     }
  491.     return(1);
  492.  
  493.  
  494.     phooey:
  495.     if (prog->do_folding)
  496.         Safefree(string);
  497.     return(0);
  498. }
  499.  
  500.  
  501. /*
  502.  - regtry - try match at specific point
  503.  */
  504. static int            /* 0 failure, 1 success */
  505. regtry(prog, string)
  506. regexp *prog;
  507. char *string;
  508. {
  509.     register int i;
  510.     register char **sp;
  511.     register char **ep;
  512.  
  513.  
  514.     reginput = string;
  515.     regstartp = prog->startp;
  516.     regendp = prog->endp;
  517.     reglastparen = &prog->lastparen;
  518.     prog->lastparen = 0;
  519.  
  520.  
  521.     sp = prog->startp;
  522.     ep = prog->endp;
  523.     if (prog->nparens) {
  524.         for (i = prog->nparens; i >= 0; i--) {
  525.             *sp++ = NULL;
  526.             *ep++ = NULL;
  527.         }
  528.     }
  529.     if (regmatch(prog->program + 1) && reginput >= regtill) {
  530.         prog->startp[0] = string;
  531.         prog->endp[0] = reginput;
  532.         return(1);
  533.     } else
  534.         return(0);
  535. }
  536.  
  537.  
  538. /*
  539.  - regmatch - main matching routine
  540.  *
  541.  * Conceptually the strategy is simple:  check to see whether the current
  542.  * node matches, call self recursively to see whether the rest matches,
  543.  * and then act accordingly.  In practice we make some effort to avoid
  544.  * recursion, in particular by going through "ordinary" nodes (that don't
  545.  * need to know whether the rest of the match failed) by a loop instead of
  546.  * by recursion.
  547.  */
  548. /* [lwall] I've hoisted the register declarations to the outer block in order to
  549.  * maybe save a little bit of pushing and popping on the stack.  It also takes
  550.  * advantage of machines that use a register save mask on subroutine entry.
  551.  */
  552. static int            /* 0 failure, 1 success */
  553. regmatch(prog)
  554. char *prog;
  555. {
  556.     register char *scan;    /* Current node. */
  557.     char *next;        /* Next node. */
  558.     register int nextchar;
  559.     register int n;        /* no or next */
  560.     register int ln;        /* len or last */
  561.     register char *s;    /* operand or save */
  562.     register char *locinput = reginput;
  563.  
  564.  
  565.     nextchar = *locinput;
  566.     scan = prog;
  567. #ifdef DEBUGGING
  568.     if (scan != NULL && regnarrate)
  569.         fprintf(stderr, "%s(\n", regprop(scan));
  570. #endif
  571.     while (scan != NULL) {
  572. #ifdef DEBUGGING
  573.         if (regnarrate)
  574.             fprintf(stderr, "%s...\n", regprop(scan));
  575. #endif
  576.  
  577.  
  578. #ifdef REGALIGN
  579.         next = scan + NEXT(scan);
  580.         if (next == scan)
  581.             next = NULL;
  582. #else
  583.         next = regnext(scan);
  584. #endif
  585.  
  586.  
  587.         switch (OP(scan)) {
  588.         case BOL:
  589.             if (locinput == regbol ? regprev == '\n' :
  590.                 ((nextchar || locinput < regeol) &&
  591.                   locinput[-1] == '\n') )
  592.             {
  593.                 /* regtill = regbol; */
  594.                 break;
  595.             }
  596.             return(0);
  597.         case EOL:
  598.             if ((nextchar || locinput < regeol) && nextchar != '\n')
  599.                 return(0);
  600.             if (!multiline && regeol - locinput > 1)
  601.                 return 0;
  602.             /* regtill = regbol; */
  603.             break;
  604.         case ANY:
  605.             if ((nextchar == '\0' && locinput >= regeol) ||
  606.               nextchar == '\n')
  607.                 return(0);
  608.             nextchar = *++locinput;
  609.             break;
  610.         case EXACTLY:
  611.             s = OPERAND(scan);
  612.             ln = *s++;
  613.             /* Inline the first character, for speed. */
  614.             if (*s != nextchar)
  615.                 return(0);
  616.             if (regeol - locinput < ln)
  617.                 return 0;
  618.             if (ln > 1 && bcmp(s, locinput, ln) != 0)
  619.                 return(0);
  620.             locinput += ln;
  621.             nextchar = *locinput;
  622.             break;
  623.         case ANYOF:
  624.             s = OPERAND(scan);
  625.             if (nextchar < 0)
  626.                 nextchar = UCHARAT(locinput);
  627.             if (s[nextchar >> 3] & (1 << (nextchar&7)))
  628.                 return(0);
  629.             if (!nextchar && locinput >= regeol)
  630.                 return 0;
  631.             nextchar = *++locinput;
  632.             break;
  633.         case ALNUM:
  634.             if (!nextchar)
  635.                 return(0);
  636.             if (!isALNUM(nextchar))
  637.                 return(0);
  638.             nextchar = *++locinput;
  639.             break;
  640.         case NALNUM:
  641.             if (!nextchar && locinput >= regeol)
  642.                 return(0);
  643.             if (isALNUM(nextchar))
  644.                 return(0);
  645.             nextchar = *++locinput;
  646.             break;
  647.         case NBOUND:
  648.         case BOUND:
  649.             if (locinput == regbol)    /* was last char in word? */
  650.                 ln = isALNUM(regprev);
  651.             else
  652.                 ln = isALNUM(locinput[-1]);
  653.             n = isALNUM(nextchar); /* is next char in word? */
  654.             if ((ln == n) == (OP(scan) == BOUND))
  655.                 return(0);
  656.             break;
  657.         case SPACE:
  658.             if (!nextchar && locinput >= regeol)
  659.                 return(0);
  660.             if (!isSPACE(nextchar))
  661.                 return(0);
  662.             nextchar = *++locinput;
  663.             break;
  664.         case NSPACE:
  665.             if (!nextchar)
  666.                 return(0);
  667.             if (isSPACE(nextchar))
  668.                 return(0);
  669.             nextchar = *++locinput;
  670.             break;
  671.         case DIGIT:
  672.             if (!isDIGIT(nextchar))
  673.                 return(0);
  674.             nextchar = *++locinput;
  675.             break;
  676.         case NDIGIT:
  677.             if (!nextchar && locinput >= regeol)
  678.                 return(0);
  679.             if (isDIGIT(nextchar))
  680.                 return(0);
  681.             nextchar = *++locinput;
  682.             break;
  683.         case REF:
  684.             n = ARG1(scan);  /* which paren pair */
  685.             s = regmystartp[n];
  686.             if (!s)
  687.                 return(0);
  688.             if (!regmyendp[n])
  689.                 return(0);
  690.             if (s == regmyendp[n])
  691.                 break;
  692.             /* Inline the first character, for speed. */
  693.             if (*s != nextchar)
  694.                 return(0);
  695.             ln = regmyendp[n] - s;
  696.             if (locinput + ln > regeol)
  697.                 return 0;
  698.             if (ln > 1 && bcmp(s, locinput, ln) != 0)
  699.                 return(0);
  700.             locinput += ln;
  701.             nextchar = *locinput;
  702.             break;
  703.  
  704.  
  705.         case NOTHING:
  706.             break;
  707.         case BACK:
  708.             break;
  709.         case OPEN:
  710.             n = ARG1(scan);  /* which paren pair */
  711.             reginput = locinput;
  712.  
  713.  
  714.             regmystartp[n] = locinput;    /* for REF */
  715.             if (regmatch(next)) {
  716.                 /*
  717.                  * Don't set startp if some later
  718.                  * invocation of the same parentheses
  719.                  * already has.
  720.                  */
  721.                 if (regstartp[n] == NULL)
  722.                     regstartp[n] = locinput;
  723.                 return(1);
  724.             } else
  725.                 return(0);
  726.             /* NOTREACHED */
  727.         case CLOSE: {
  728.                 n = ARG1(scan);  /* which paren pair */
  729.                 reginput = locinput;
  730.  
  731.  
  732.                 regmyendp[n] = locinput;    /* for REF */
  733.                 if (regmatch(next)) {
  734.                     /*
  735.                      * Don't set endp if some later
  736.                      * invocation of the same parentheses
  737.                      * already has.
  738.                      */
  739.                     if (regendp[n] == NULL) {
  740.                         regendp[n] = locinput;
  741.                         if (n > *reglastparen)
  742.                             *reglastparen = n;
  743.                     }
  744.                     return(1);
  745.                 } else
  746.                     return(0);
  747.             }
  748.             /*NOTREACHED*/
  749.         case BRANCH: {
  750.                 if (OP(next) != BRANCH)        /* No choice. */
  751.                     next = NEXTOPER(scan);    /* Avoid recursion. */
  752.                 else {
  753.                     do {
  754.                         reginput = locinput;
  755.                         if (regmatch(NEXTOPER(scan)))
  756.                             return(1);
  757. #ifdef REGALIGN
  758.                         /*SUPPRESS 560*/
  759.                         if (n = NEXT(scan))
  760.                             scan += n;
  761.                         else
  762.                             scan = NULL;
  763. #else
  764.                         scan = regnext(scan);
  765. #endif
  766.                     } while (scan != NULL && OP(scan) == BRANCH);
  767.                     return(0);
  768.                     /* NOTREACHED */
  769.                 }
  770.             }
  771.             break;
  772.         case CURLY:
  773.             ln = ARG1(scan);  /* min to match */
  774.             n  = ARG2(scan);  /* max to match */
  775.             scan = NEXTOPER(scan) + 4;
  776.             goto repeat;
  777.         case STAR:
  778.             ln = 0;
  779.             n = 0;
  780.             scan = NEXTOPER(scan);
  781.             goto repeat;
  782.         case PLUS:
  783.             /*
  784.              * Lookahead to avoid useless match attempts
  785.              * when we know what character comes next.
  786.              */
  787.             ln = 1;
  788.             n = 0;
  789.             scan = NEXTOPER(scan);
  790.             repeat:
  791.             if (OP(next) == EXACTLY)
  792.                 nextchar = *(OPERAND(next)+1);
  793.             else
  794.                 nextchar = -1000;
  795.             reginput = locinput;
  796.             n = regrepeat(scan, n);
  797.             if (!multiline && OP(next) == EOL && ln < n)
  798.                 ln = n;            /* why back off? */
  799.             while (n >= ln) {
  800.                 /* If it could work, try it. */
  801.                 if (nextchar == -1000 || *reginput == nextchar)
  802.                     if (regmatch(next))
  803.                         return(1);
  804.                 /* Couldn't or didn't -- back up. */
  805.                 n--;
  806.                 reginput = locinput + n;
  807.             }
  808.             return(0);
  809.         case END:
  810.             reginput = locinput; /* put where regtry can find it */
  811.             return(1);    /* Success! */
  812.         default:
  813.             printf("%x %d\n",scan,scan[1]);
  814.             FAIL("regexp memory corruption");
  815.         }
  816.  
  817.  
  818.         scan = next;
  819.     }
  820.  
  821.  
  822.     /*
  823.      * We get here only if there's trouble -- normally "case END" is
  824.      * the terminating point.
  825.      */
  826.     FAIL("corrupted regexp pointers");
  827.     /*NOTREACHED*/
  828. #ifdef lint
  829.     return 0;
  830. #endif
  831. }
  832.  
  833.  
  834. /*
  835.  - regrepeat - repeatedly match something simple, report how many
  836.  */
  837. /*
  838.  * [This routine now assumes that it will only match on things of length 1.
  839.  * That was true before, but now we assume scan - reginput is the count,
  840.  * rather than incrementing count on every character.]
  841.  */
  842. static int
  843. regrepeat(p, max)
  844. char *p;
  845. int max;
  846. {
  847.     register char *scan;
  848.     register char *opnd;
  849.     register int c;
  850.     register char *loceol = regeol;
  851.  
  852.  
  853.     scan = reginput;
  854.     if (max && max < loceol - scan)
  855.         loceol = scan + max;
  856.     opnd = OPERAND(p);
  857.     switch (OP(p)) {
  858.     case ANY:
  859.         while (scan < loceol && *scan != '\n')
  860.             scan++;
  861.         break;
  862.     case EXACTLY:        /* length of string is 1 */
  863.         opnd++;
  864.         while (scan < loceol && *opnd == *scan)
  865.             scan++;
  866.         break;
  867.     case ANYOF:
  868.         c = UCHARAT(scan);
  869.         while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
  870.             scan++;
  871.             c = UCHARAT(scan);
  872.         }
  873.         break;
  874.     case ALNUM:
  875.         while (scan < loceol && isALNUM(*scan))
  876.             scan++;
  877.         break;
  878.     case NALNUM:
  879.         while (scan < loceol && !isALNUM(*scan))
  880.             scan++;
  881.         break;
  882.     case SPACE:
  883.         while (scan < loceol && isSPACE(*scan))
  884.             scan++;
  885.         break;
  886.     case NSPACE:
  887.         while (scan < loceol && !isSPACE(*scan))
  888.             scan++;
  889.         break;
  890.     case DIGIT:
  891.         while (scan < loceol && isDIGIT(*scan))
  892.             scan++;
  893.         break;
  894.     case NDIGIT:
  895.         while (scan < loceol && !isDIGIT(*scan))
  896.             scan++;
  897.         break;
  898.     default:        /* Oh dear.  Called inappropriately. */
  899.         FAIL("internal regexp foulup");
  900.         /* NOTREACHED */
  901.     }
  902.  
  903.  
  904.     c = scan - reginput;
  905.     reginput = scan;
  906.  
  907.  
  908.     return(c);
  909. }
  910.  
  911.  
  912. /*
  913.  - regnext - dig the "next" pointer out of a node
  914.  *
  915.  * [Note, when REGALIGN is defined there are two places in regmatch()
  916.  * that bypass this code for speed.]
  917.  */
  918. char *
  919. regnext(p)
  920. register char *p;
  921. {
  922.     register int offset;
  923.  
  924.  
  925.     if (p == ®dummy)
  926.         return(NULL);
  927.  
  928.  
  929.     offset = NEXT(p);
  930.     if (offset == 0)
  931.         return(NULL);
  932.  
  933.  
  934. #ifdef REGALIGN
  935.     return(p+offset);
  936. #else
  937.     if (OP(p) == BACK)
  938.         return(p-offset);
  939.     else
  940.         return(p+offset);
  941. #endif
  942. }
  943.